﻿using InfonavaEye.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye
{
    public partial class ServerForm : Form
    {
        TcpListener tcpListener = null;
        public ServerForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(CreateListen));
            thread.Start();
        }

        //委托
        protected delegate void ShowContentDelegate(string content);
        private void CreateListen()
        {
            try
            {
                tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8869);
                tcpListener.Start();
                MessageBox.Show("已成功在8869监听");

                while (true)
                {
                    TcpClient client = tcpListener.AcceptTcpClient();
                    NetworkStream stream = client.GetStream();
                    var remote = client.Client.RemoteEndPoint;
                    Console.WriteLine("0000");

                    while (true)
                    {
                        try
                        {
                            Console.WriteLine("1111");
                            byte[] message = new byte[1024];
                            int bytesRead;
                            //blocks until a client sends a message
                            bytesRead = stream.Read(message, 0, 1024);
                            var result = Encoding.UTF8.GetString(message, 0, bytesRead);
                            ShowContentDelegate myContent = new ShowContentDelegate(ShowContent);           //绑定委托与方法
                            Invoke(myContent, result);

                            //回应消息
                            byte[] buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes("服务端返回消息：" + result);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                        catch (Exception ex)
                        {
                            //a socket error has occured
                            Console.WriteLine($"发生异常{ex.Message},{ex.StackTrace}");
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生异常{0}{1}", ex.Message, ex.StackTrace);
                MessageBox.Show("发生异常，" + ex.Message);
            }
        }
        public void ShowContent(string content)
        {
            this.textBox1.Text += content + "\r\n";
        }
    }


}
